package com.thinkgem.jeesite.modules.monitor.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;


/**
 * 主机监控Controller
 * 
 * @author WangYuan
 * @date 2016年7月26日下午5:27:21
 */
@Controller
@RequestMapping(value = "${adminPath}/server/monitor")
public class ServerMonitorController extends BaseController {

    /**
     * 封装监控指标数据,跳转监控页面
     * 
     * @param model
     * @param hostName
     * @param type
     * @return
     */
    @RequestMapping(value = {"/realtime", ""})
    public String showChart(@RequestParam String hostName, @RequestParam String type, Model model,
            RedirectAttributes redirectAttributes) {
        if (!type.equals("type_windows") && !type.equals("type_linux")) { // 除服务器类型直接跳转历史页面
            redirectAttributes.addAttribute("type", type);
            redirectAttributes.addAttribute("hostName", hostName);
            return String.format("redirect:%s/zabbix/api/history.view", adminPath);
        }
        List<String> seriesMap = new ArrayList<>(); // key-label
        Map<String, Integer> indexMap = new HashMap<>(); // key-sort
        List<Dict> list = DictUtils.getDictList(type);
        for (Dict dict : list) {
            seriesMap.add(dict.getLabel());
            indexMap.put(dict.getValue(), dict.getSort());
        }
        model.addAttribute("type", type);
        model.addAttribute("hostName", hostName);
        model.addAttribute("indexMap", indexMap);
        model.addAttribute("seriesMap", seriesMap);
        model.addAttribute("socketAddress", socketAddress);
        return "modules/monitor/serverMonitor";
    }

}
